// robsingle.js
var app = getApp()
import { formatData, formatTime, outputmoney } from "../../utils/util.js";
import { showToast } from "../../utils/modal.js";
var date = new Date();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    weeks_ch: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    weeks_data: [],
    activeIndex: '',
    weekObj: {
      page: 1,
      time: null,
      limit: ""
    },
    scheduleList: [],
    user:null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    self.setData({
      user: wx.getStorageSync('wechat_user')
    })
    self.data.weekObj.time = self.transferDate(date)
    self.getData()
   
    self.measureList()
    self.data.activeIndex = self.data.weekObj.time
    console.log(self.data.activeIndex)
  },
  //获取当前一周的日期
  getData: function () {
    var dateArr = []
    var this_day = date.getDay(); //今天是这周的第几天
    var step_s = -this_day + 1; //上周日距离今天的天数（负数表示）
    var step_m = 7 - this_day  // 本周日距离今天的天数
    if (this_day == 0) {
      step_s = -7; // 如果今天是周日
    }
    var thisTime = date.getTime()
    var monday = new Date(thisTime + step_s * 24 * 3600 * 1000)
    var sunday = new Date(thisTime + step_m * 24 * 3600 * 1000)
    var starttime = this.transferDate(monday) //本周一的日期 （起始日期）
    var endtime = this.transferDate(sunday)  //本周日的日期 （结束日期）
    var date1 = this.dataMiddle(starttime)
    var date2 = this.dataMiddle(endtime)
    if (date1 > date2) {
      var tempDate = date1;
      date1 = date2;
      date2 = tempDate;
    }
    console.log(date1.getFullYear(), date2.getFullYear())
    console.log(date1.getDate(), date2.getDate())
    console.log(date1.getMonth(), date2.getMonth())
    var i = 0;
    while (!(date1.getFullYear() == date2.getFullYear()
       && date1.getDate() == date2.getDate())) {
// && date1.getMonth() == date2.getMonth()
        
      var dayStr = date1.getDate().toString()
      var dayStr2 = date2.getDate().toString()
      var monStr = date1.getMonth() + 1
      var monStr2 = date2.getMonth() + 1
      if (dayStr.length == 1) {
        dayStr = "0" + dayStr;
      }
      if (dayStr2.length == 1) {
        dayStr2 = "0" + dayStr2;
      }
      if (monStr < 10) {
        monStr = "0" + monStr;
      }
      if (monStr2 < 10) {
        monStr2 = "0" + monStr2;
      }
      var dataObj = { datas: null, day: null, thisDay: null }
      dataObj.datas = date1.getFullYear() + "-" + monStr + "-" + dayStr
      dataObj.day = dayStr
      dataObj.thisDay = this_day
      dateArr[i] = dataObj
      var dataObj2 = { datas: null, day: null, thisDay: null }
      dataObj2.datas = date2.getFullYear() + "-" + monStr2 + "-" + dayStr2
      dataObj2.day = dayStr2
      dataObj2.thisDay = this_day
      dateArr.push(dataObj2)
      i++;
      date1.setDate(date1.getDate() + 1);
    }
    // switch (this_day){
    //   case 0: this.data.activeIndex = this_day; break
    //   case 1: this.data.activeIndex = this_day; break
    //   case 2: this.data.activeIndex = this_day; break
    //   case 3: this.data.activeIndex = this_day; break
    //   case 4: this.data.activeIndex = this_day; break
    //   case 5: this.data.activeIndex = this_day; break
    //   case 6: this.data.activeIndex = this_day; break
    // } 
    this.setData({
      weeks_data: dateArr
    })
    for (var i in this.data.weeks_data) {
      if (this.data.weekObj.time === this.data.weeks_data[i].datas) {
        this.setData({
          activeIndex: i
        })
        console.log(this.data.activeIndex)
      }
    }
  },
  //日期拼接
  transferDate: function (date) {
    var yearTemp = date.getFullYear()
    var monthTemp = date.getMonth() + 1
    var dayTemp = date.getDate()
    if (parseInt(monthTemp) < 10) {
      monthTemp = "0" + monthTemp
    }
    if (parseInt(dayTemp) < 10) {
      dayTemp = "0" + dayTemp
    }
    return yearTemp + "-" + monthTemp + "-" + dayTemp;
  },

  //日期段
  dataMiddle: function (datestr) {
    var tempDate = new Date()
    var list = datestr.split("-")
    tempDate.setFullYear(list[0])
    tempDate.setMonth(list[1] - 1)
    tempDate.setDate(list[2])
    return tempDate
  },
  //获取本周安排
  weekList: function (dataType) {
    let self = this
    let timeObj = self.data.weekObj
    if (dataType) {
      timeObj.time = dataType.currentTarget.dataset.day
      self.setData({
        activeIndex: dataType.currentTarget.id
      })
    }
    app.query("web/orders/calendar_orders", timeObj).then(res => {
      if (res.code === 200) {
        console.log(res)
        self.setData({
          scheduleList: formatData(res.orders)
        })
      }
    })
  },
  //获取测量所有单
  measureList: function () {
    let self = this
    // if (self.data.user != null || self.data.user!=undefined) {
      app.query("web/orders", { type: 'service' }).then(res => {
        if (res.code === 200) {
          let formatOrders = formatData(res.orders)
          let formatOrdersObj = outputmoney(formatOrders)
          self.setData({
            orderList: formatOrders,
          })
          self.weekList()
          console.log(this.data.orderList)
        }
      })
    // }else{
    //   wx.reLaunch({ url: '/pages/login/login'})
    // }
  },
  //抢单
  robsingle: function (event) {
    let self = this
    let dataset = event.currentTarget.dataset
    app.query("web/orders/" + dataset.id + "/compete_order", "", "post").then(res => {
      if (res.code === 200) {
        console.log(res)
        self.data.orderList.splice(dataset.index, 1);
        self.setData({
          orderList: self.data.orderList
        });
        showToast("抢单成功")
      }else{
        showToast(res.msg)
      }
    })
  },
})